<template>
  <div class="users">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <div style="margin: 15px 0;">
      <el-input
        v-model="query"
        placeholder="请输入内容"
        class="input-with-select"
        style="width:300px;margin-rigth:15px"
        @keyup.enter.native="search"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      </el-input>
      <el-button
        type="success"
        @click="addDialogFormVisible = true"
      >添加用户</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :data="userList"
      border
      style="width: 100%"
    >
      <el-table-column
        type="index"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="姓名"
        width="280"
      >
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="280"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话"
      >
      </el-table-column>
      <el-table-column
        label="用户状态"
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeUserState(scope.row.id,scope.row.mg_state)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="分配角色"
            placement="top"
          >
            <el-button
              type="primary"
              icon="el-icon-share"
              @click="showGrandDialog(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
          >
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="deluserbyid(scope.row.id)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogFormVisible" >
      <el-form :model="addForm" :label-width="'80px'" :rules="rules" ref="addForm" >
        <el-form-item label="用户名" prop="username" >
          <el-input
            v-model="addForm.username"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            v-model="addForm.password"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="邮箱"
          prop="email"
        >
          <el-input
            v-model="addForm.email"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="电话"
          prop="mobile"
        >
          <el-input
            v-model="addForm.mobile"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addUser"
        >确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分配角色对话框 -->
    <el-dialog title="分配角色" :visible.sync="grantdialogFormVisible">
  <el-form :model="grantForm" :label-width="'120px'">
    <el-form-item label="用户名">
      <el-input v-model="grantForm.username" auto-complete="off" disabled="" style="width:100px"></el-input>
    </el-form-item>
    <el-form-item label="角色">
      <el-select v-model="grantForm.rid" clearable placeholder="请选择">
        <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="grantdialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="grantRoleSubmit">确 定</el-button>
  </div>
</el-dialog>
  <!-- 编辑用户对话框 -->
  <el-dialog title="编辑用户" :visible.sync="editdialogFormVisible">
  <el-form :model="editForm" :label-width="'120px'" :rules="rules" ref="editForm">
    <el-form-item label="用户名">
      <el-input v-model="editForm.username" auto-complete="off" disabled="" style="width:100px"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="editForm.email" auto-complete="off" >
      </el-input>
    </el-form-item>
    <el-form-item label="电话" prop="mobile">
      <el-input v-model="editForm.mobile" auto-complete="off" >
      </el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editdialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserSubmit">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>
<script>
import { getUserList, addUser, delUser, updateUserState, grantUserRole, editUser } from '@/api/user_api.js'
import { getAllRoleLlist } from '@/api/role_api.js'
export default {
  data () {
    return {
      editdialogFormVisible: false,
      editForm: {
        id: '',
        username: '',
        email: '',
        mobile: ''
      },
      value4: '',
      roleList: '',
      grantForm: {
        username: '',
        id: '',
        rid: ''
      },
      grantdialogFormVisible: false,

      // 标记添加用户对话框显示隐藏
      addDialogFormVisible: false,
      // 添加用户数据对象
      addForm: {
        username: '',
        password: '',
        email: 'b@g.com',
        mobile: '1360193501'
      },
      total: 0,
      // 搜索关键字
      query: '',
      // 当前页码
      pagenum: 1,
      // 每页显示几条数据
      pagesize: 3,
      // value2: true,
      userList: [],
      // 添加新增用户单元素的验证规则
      rules: {
        username: [
          { required: true, message: '请填写用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请填写邮箱', trigger: 'blur', pattern: /\w+[@]\w+[.]\w{2,3}/ }
        ],
        mobile: [
          { required: true, message: '请填写手机号', trigger: 'blur' },
          { pattern: /^1[345789]\d{9}$/, message: '请填写正确的手机号（11位数字）', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.init()
    // 获取所有角色列表数据
    getAllRoleLlist()
      .then(res => {
        if (res.data.meta.status === 200) {
          this.roleList = res.data.data
        }
      })
  },
  methods: {
    // 显示编辑用户对话框
    showEditDialog (row) {
      // console.log(row)
      this.editdialogFormVisible = true
      this.editForm.username = row.username
      this.editForm.id = row.id
      this.editForm.email = row.email
      this.editForm.mobile = row.mobile
    },
    // 编辑用户提交
    editUserSubmit () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          editUser(this.editForm).then(res => {
            if (res.data.meta.status === 200) {
              this.$message({
                type: 'success',
                message: res.data.meta.msg
              })
              this.editdialogFormVisible = false
              this.init()
            }
          })
        } else {
          this.$message({
            type: 'warning',
            message: '用户输入不合法'
          })
        }
      })
    },

    // 分配角色提交
    grantRoleSubmit () {
      // 判断是否选择角色
      if (!this.grantForm.rid) {
        this.$message({
          type: 'warning',
          message: '请先选择角色'
        })
        return false
      }

      grantUserRole(this.grantForm.id, this.grantForm.rid)
        .then(res => {
          if (res.data.meta.status === 200) {
            this.$message({
              type: 'warning',
              message: res.data.meta.msg
            })
            this.grantdialogFormVisible = false
          }
        })
    },
    // 显示分配角色对话框
    showGrandDialog (row) {
      // console.log(row)
      this.grantdialogFormVisible = true
      this.grantForm.username = row.username
      this.grantForm.id = row.id
      this.grantForm.rid = row.rid
    },
    // 修改用户状态
    changeUserState (id, type) {
      updateUserState(id, type).then(res => {
        if (res.data.meta.status === 200) {
          this.$message({
            type: 'success',
            message: res.data.meta.msg
          })
        }
      })
    },

    // 删除单个用户
    deluserbyid (id) {
      // 给出删除提示
      this.$confirm('此操作将永久删除该文件, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delUser(id)
            .then(res => {
              if (res.data.meta.status === 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.pagenum = Math.ceil(this.total - 1 / this.pagesize) < this.pagenum
                  ? --this.pagenum
                  : this.pagenum
                this.init()
              }
            })
            .catch(err1 => {
              this.$message({
                type: 'error',
                message: '删除失败'
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 添加用户数据
    addUser () {
      // 1.再次实现数据的验证
      this.$refs.addForm.validate(valid => {
        if (valid) {
          addUser(this.addForm)
            .then(res => {
              if (res.data.meta.status === 201) {
                // 让对话框隐藏
                this.addDialogFormVisible = false
                // 重置表单元素数据
                this.$refs.addForm.resetFields()
                // 实现数据 刷新
                this.init()
              } else {
                this.$message({
                  type: 'error',
                  message: res.data.meta.msg
                })
              }
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          return false
        }
      })
    },

    // 搜索
    search () {
      this.pagenum = 1
      this.init()
    },
    init () {
      // 获取所有用户数据
      getUserList({ query: this.query, pagenum: this.pagenum, pagesize: this.pagesize })
        .then(res => {
          console.log(res)
          this.userList = res.data.data.users
          this.total = res.data.data.total
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 切换size下拉菜单列表时触发

    handleSizeChange (val) {
      // 重置pagesize
      this.pagesize = val
      // 再获取数据
      this.init()
    },
    handleCurrentChange (val) {
      // 重置pagenum
      this.pagenum = val
      // 再获取数据
      this.init()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
